home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / BOPEN.C < prev    next >
Text File  |  1991-09-23  |  5KB  |  184 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)bopen.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13. #ifdef AC_STRING
  14. #include <string.h>
  15. #endif
  16.  
  17. /* local headers */
  18. #include "blkio_.h"
  19.  
  20. /* block file control structure table definition */
  21. BLKFILE biob[BOPEN_MAX];
  22.  
  23. /*man---------------------------------------------------------------------------
  24. NAME
  25.      bopen - open a block file
  26.  
  27. SYNOPSIS
  28.      #include <blkio.h>
  29.  
  30.      BLKFILE *bopen(filename, type, hdrsize, blksize, bufcnt)
  31.      const char *filename;
  32.      const char *type;
  33.      size_t hdrsize;
  34.      size_t blksize;
  35.      size_t bufcnt;
  36.  
  37. DESCRIPTION
  38.      The bopen function opens the file named by filename as a block
  39.      file.  A pointer to the BLKFILE structure associated with file is
  40.      returned.
  41.  
  42.      filename points to a character string that contains the name of
  43.      the file to be opened.
  44.  
  45.      type is a character string having one of the following values:
  46.  
  47.           "r"            open for reading
  48.           "r+"           open for update (reading and writing)
  49.           "w+"           truncate or create for update
  50.           "c"            create for update
  51.  
  52.      If type is "r" or "r+" and the file does not exist, bopen will
  53.      fail.  If type is "c" and the file already exists, bopen will
  54.      fail.
  55.  
  56.      hdrsize is the size of the file header.  If there is no file
  57.      header, specify a value of 0 for hdrsize.
  58.  
  59.      blksize is the size of the blocks.
  60.  
  61.      bufcnt is the number of blocks to for which to create buffer
  62.      storage.  For unbuffered operation, specify a value of 0 for
  63.      bufcnt.
  64.  
  65.      bopen will fail if one or more of the following is true:
  66.  
  67.      [EEXIST]       type is "c" and the named file exists.
  68.      [EINVAL]       filename or type is the NULL pointer.
  69.      [EINVAL]       type is not "r", "r+", "w+", or "c".
  70.      [EINVAL]       blksize is 0.
  71.      [ENOENT]       type is "r" or "r+" and the named file
  72.                     does not exist.
  73.      [BEMFILE]      The maximum number of block files is
  74.                     already open.
  75.  
  76. SEE ALSO
  77.      bclose.
  78.  
  79. DIAGNOSTICS
  80.      bopen returns a NULL pointer on failure, and errno is set to
  81.      indicate the error.
  82.  
  83. NOTES
  84.      If the file being opened does not end on a block boundary, the
  85.      trailing partial block is ignored; blkio considers the end of the
  86.      last complete block as the end of the file.
  87.  
  88. ------------------------------------------------------------------------------*/
  89. #ifdef AC_PROTO
  90. BLKFILE *bopen(const char *filename, const char *type, size_t hdrsize, size_t blksize, size_t bufcnt)
  91. #else
  92. BLKFILE *bopen(filename, type, hdrsize, blksize, bufcnt)
  93. const char *filename;
  94. const char *type;
  95. size_t hdrsize;
  96. size_t blksize;
  97. size_t bufcnt;
  98. #endif
  99. {
  100.     BLKFILE *    bp    = NULL;
  101.     int        terrno    = 0;
  102.  
  103.     /* validate arguments */
  104.     if (filename == NULL || type == NULL || blksize == 0) {
  105.         errno = EINVAL;
  106.         return NULL;
  107.     }
  108.  
  109.     /* find free slot in biob table */
  110.     for (bp = biob; bp < biob + BOPEN_MAX; ++bp) {
  111.         if (!(bp->flags & BIOOPEN)) {
  112.             break;        /* found */
  113.         }
  114.     }
  115.     if (bp > (biob + BOPEN_MAX - 1)) {
  116.         errno = BEMFILE;
  117.         return NULL;        /* no free slots */
  118.     }
  119.  
  120.     /* set biob flags */
  121.     if (strcmp(type, BF_READ) == 0) {
  122.         bp->flags = BIOREAD;
  123.     } else if (strcmp(type, BF_RDWR) == 0) {
  124.         bp->flags = BIOREAD | BIOWRITE;
  125.     } else if (strcmp(type, BF_CREATE) == 0) {
  126.         bp->flags = BIOREAD | BIOWRITE;
  127.     } else if (strcmp(type, BF_CRTR) == 0) {
  128.         bp->flags = BIOREAD | BIOWRITE;
  129.     } else {
  130.         errno = EINVAL;
  131.         return NULL;
  132.     }
  133.  
  134.     /* open file */
  135.     if (b_uopen(bp, filename, type) == -1) {
  136.         if ((errno != EEXIST) && (errno != ENOENT)) BEPRINT;
  137.         memset(bp, 0, sizeof(*biob));
  138.         bp->flags = 0;
  139.         return NULL;
  140.     }
  141.  
  142.     /* initialize */
  143.     bp->hdrsize = hdrsize;
  144.     bp->blksize = blksize;
  145.     bp->bufcnt = bufcnt;
  146.     bp->endblk = 0;
  147.     bp->most = 0;
  148.     bp->least = 0;
  149.     bp->blockp = NULL;
  150.     bp->blkbuf = NULL;
  151.     if (b_uendblk(bp, &bp->endblk) == -1) {
  152.         BEPRINT;
  153.         terrno = errno;
  154.         b_uclose(bp);
  155.         memset(bp, 0, sizeof(*biob));
  156.         bp->flags = 0;
  157.         errno = terrno;
  158.         return NULL;
  159.     }
  160.     /* allocate memory for bp */
  161.     if (b_alloc(bp) == -1) {
  162.         BEPRINT;
  163.         terrno = errno;
  164.         b_uclose(bp);
  165.         memset(bp, 0, sizeof(*biob));
  166.         bp->flags = 0;
  167.         errno = terrno;
  168.         return NULL;
  169.     }
  170.     /* initialize buffer storage */
  171.     if (b_initlist(bp) == -1) {
  172.         BEPRINT;
  173.         terrno = errno;
  174.         b_free(bp);
  175.         b_uclose(bp);
  176.         memset(bp, 0, sizeof(*biob));
  177.         bp->flags = 0;
  178.         errno = terrno;
  179.         return NULL;
  180.     }
  181.  
  182.     return bp;
  183. }
  184.